<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\ReplyArticleModel;
use app\common\model\ReplyAssetModel;
class WechatarticleController extends Controller
{
    public function index()
    {
        if($this->request->isAjax())
        {
            $reply=new ReplyArticleModel();
            $post=$this->request->post();
            $result=$reply->getArticles($post["page"],$post["limit"]);   
            return \pagejson($result["data"],$result["count"]);
        }
        return view();
    }
    
    public function create()
    {
        if($this->request->isAjax())
        {
            $reply=new ReplyArticleModel();
            $post=$this->request->post();
            $data=http_mergeParams($post);
            
            if(count($data)>8){
                return \zbcjson(false,"最多8条图文");
            }

            if(count($data)>=1){
                $id=$reply->addArticles($data);
                if(!empty($id)){
                    return \zbcjson();
                }
            }
            return \zbcjson(false,"保存失败");
        }
        
        return view();
    }

    public function update()
    {
        if($this->request->isAjax())
        {
            $artilce=new ReplyArticleModel();
            $post=$this->request->post();
            $data=http_mergeParams($post);

            if(count($data)>8){
                return \zbcjson(false,"最多8条图文");
            }

            if(count($data)>=1 && array_key_exists("id",$data[0])){
                $result=$artilce->getArticleWithImage($data["id"]);
                if(!$result)
                {
                    return \zbcjson(false,"数据不存在");
                }

                $id=$artilce->addArticles($data);
                if(!empty($id)){
                    return \zbcjson();
                }
            }
            return \zbcjson(false,"编辑失败");
        }

        $get=$this->request->get();

        if(!array_key_exists("id",$get))
        {
            return $this->display("数据不存在");
        }
        $artilce=new ReplyArticleModel;
        $result=$artilce->getArticleWithImage($get["id"]);

        if(!$result)
        {
            return $this->display("数据不存在");
        }
        
        $models=array($result);
        foreach($result->articles as $article=>$value){
            array_push($models,$value);
        }
        $this->assign("model",$models);
        return view();
    }

    public function delete()
    {
        $post=$this->request->post();
        if(!array_key_exists("data",$post))
        {
            return \zbcjson(false,"数据不存在");
        }
        $reply=new ReplyArticleModel;
        $id=$reply->deleteArticles($post["data"]);
        if(!empty($id)){
            
            return \zbcjson();
        }
        return \zbcjson(false,"删除失败");
    }

    public function list(){
        if($this->request->isAjax())
        {
            $reply=new ReplyArticleModel();
            $post=$this->request->post();
            $result=$reply->getArticles($post["page"]
                ,$post["limit"]
                ,"id,type,title,digest,newsId,imageAssetId,mediaImage,path"
            );   
            return \pagejson($result["data"],$result["count"]);
        }
        return view();
    }

    public function images(){
        if ($this->request->isAjax()) 
        {
            $assets=new ReplyAssetModel();
            $post=$this->request->post();
            $result=$assets->page($post["page"]
                ,$post["limit"]
                ,["type"=>"image"]
                ,['created'=>'desc']
            );
            return \pagejson($result["data"], $result["count"]);
        }

        return view();
    }

    public function test(){
        return view();
    }
}